module.exports = (root, opts) => {
    const {units} = opts;
    const covertRule = opts.rule[opts.platform].map(v => {
        const _s = v.split("2");
        return {
            key: _s[0],
            covert: _s[1]
        };
    }) || [];

    root.walkRules(postrule => {
        // px rem等单位进行转换规则替换
        if (covertRule.length) {
            postrule.walkDecls(decl => {
                covertRule.forEach(v => {
                    const _times = units[`${v.key}2${v.covert}`];
                    const _regx = new RegExp(`([\\d\\.]+)${v.key}`);

                    if (_regx.test(decl.value) && typeof _times === "number") {
                        const _m = decl.value.match(_regx);
                        if (_m[1] > opts.threshold[opts.platform]) {
                            decl.value = decl.value.replace(
                                _m[0],
                                `${_m[1] * _times}${v.covert}`
                            );
                        }
                    }
                });
                // PX to px
                if (/PX$/.test(decl.value)) {
                    const _m = decl.value.match(/([\d\.]+)PX/);
                    decl.value = decl.value.replace(_m[0], parseFloat(_m[1]) + "px");
                }
            });
        }


        // 以下为选择器的替换

        // 替换:为_
        if (postrule.selector.includes("\\:")) {
            postrule.selectors = postrule.selectors.map(v => v.replace(/\\:/g, opts.selectorRule[":"]));
        }
        // 替换.5为_half
        if (postrule.selector.includes("\\.5")) {
            postrule.selectors = postrule.selectors.map(v =>
                v.replace(/\\\.5/g, opts.selectorRule[".5"])
            );
        }
        // 替换/为_
        if (postrule.selector.includes("\\/")) {
            postrule.selectors = postrule.selectors.map(v => v.replace(/\\\//g, opts.selectorRule["/"]));
        }
    });
};
